//
//  WoFaYunDetailTimeLineCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/10/12.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "WoFaYunDetailTimeLineCell.h"

@interface WoFaYunDetailTimeLineCell ()

@property (nonatomic, strong) UIView *bgView;

@end

@implementation WoFaYunDetailTimeLineCell

+ (instancetype)WoFaYunDetailTimeLineCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"WoFaYunDetailTimeLineCell";
    WoFaYunDetailTimeLineCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[WoFaYunDetailTimeLineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [UIView new];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
    }
    return self;
}

-(void)setModel:(WoFaYunDetailModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.bgView.nim_left = 15;
    self.bgView.nim_top = 10;
    self.bgView.nim_width = ScreenW - 30;
    self.bgView.nim_height = 20 + 30 * self.model.status.count;
    
    [self.bgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    __block CGFloat viewTop = 10;
    [self.model.status enumerateObjectsUsingBlock:^(WoFaYunDetailTimeLineModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        WoFaYunDetailTimeLineView *view = [[WoFaYunDetailTimeLineView alloc] initWithFrame:CGRectMake(15, viewTop, self.bgView.nim_width - 30, 30)];
        view.title = obj.value;
        view.time = obj.time;
        view.isSelected = obj.selected;
        if (idx == 0) {
            view.isFirst = YES;
        }else {
            view.isFirst = NO;
        }
        if (idx == self.model.status.count - 1) {
            view.isLast = YES;
        }else {
            view.isLast = NO;
        }
        [self.bgView addSubview:view];
        viewTop = view.nim_bottom;
    }];
    
    self.height = self.bgView.nim_bottom;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@interface WoFaYunDetailTimeLineView ()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UILabel *topLine;
@property (nonatomic, strong) UILabel *botLine;
@property (nonatomic, strong) UILabel *midDot;
@property (nonatomic, strong) UIImageView *selectImgView;

@end

@implementation WoFaYunDetailTimeLineView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 7.5, 75, 15)];
        self.titleLab.textAlignment = NSTextAlignmentCenter;
        self.titleLab.font = [UIFont systemFontOfSize:14];
        self.titleLab.textColor = HexColor(0x9C9DB3);
        [self addSubview:self.titleLab];
        
        self.midDot = [UILabel new];
        self.midDot.nim_width = 10;
        self.midDot.nim_height = 10;
        self.midDot.nim_left = self.titleLab.nim_right + 7;
        self.midDot.nim_centerY = self.titleLab.nim_centerY;
        self.midDot.layer.masksToBounds = YES;
        self.midDot.layer.cornerRadius = 5;
        self.midDot.backgroundColor = HexColor(0xF2F2F5);
        [self addSubview:self.midDot];
        
        self.topLine = [UILabel new];
        self.topLine.backgroundColor = HexColor(0xF2F2F5);
        self.topLine.nim_top = 0;
        self.topLine.nim_width = 2;
        self.topLine.nim_centerX = self.midDot.nim_centerX;
        self.topLine.nim_height = self.midDot.nim_top - 2;
        [self addSubview:self.topLine];
        
        self.botLine = [UILabel new];
        self.botLine.backgroundColor = HexColor(0xF2F2F5);
        self.botLine.nim_top = self.midDot.nim_bottom + 2;
        self.botLine.nim_width = 2;
        self.botLine.nim_centerX = self.midDot.nim_centerX;
        self.botLine.nim_height = frame.size.height - self.botLine.nim_top;
        [self addSubview:self.botLine];
        
        self.timeLab = [[UILabel alloc] initWithFrame:CGRectMake(20 + self.midDot.nim_right, 7.5, frame.size.width - (20 + self.midDot.nim_right), 15)];
        self.timeLab.textAlignment = NSTextAlignmentLeft;
        self.timeLab.font = [UIFont systemFontOfSize:14];
        self.timeLab.textColor = HexColor(0x9C9DB3);
        [self addSubview:self.timeLab];
        
        self.selectImgView = [UIImageView new];
        self.selectImgView.nim_width = 24;
        self.selectImgView.nim_height = 24;
        self.selectImgView.nim_centerX = self.midDot.nim_centerX;
        self.selectImgView.nim_centerY = self.midDot.nim_centerY;
        self.selectImgView.image = [UIImage imageNamed:@"icon_map_car_status_selected"];
        self.selectImgView.hidden = YES;
        [self addSubview:self.selectImgView];
    }
    return self;
}

-(void)setTitle:(NSString *)title {
    _title = title;
    self.titleLab.text = title;
}

-(void)setTime:(NSString *)time {
    _time = time;
    self.timeLab.text = time;
}

-(void)setIsFirst:(BOOL)isFirst {
    _isFirst = isFirst;
    self.topLine.hidden = isFirst;
}

-(void)setIsLast:(BOOL)isLast {
    _isLast = isLast;
    self.botLine.hidden = isLast;
}

-(void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    self.selectImgView.hidden = !isSelected;
    if (isSelected) {
        self.titleLab.nim_height = 19;
        self.titleLab.nim_centerY = self.selectImgView.nim_centerY;
        self.titleLab.font = BoldFontOfSize(18);
        self.titleLab.textColor = HexColor(0x3D68EB);
        
        self.timeLab.nim_height = 19;
        self.timeLab.nim_centerY = self.selectImgView.nim_centerY;
        self.timeLab.font = BoldFontOfSize(18);
        self.timeLab.textColor = HexColor(0x2B2C44);
    }else {
        self.titleLab.nim_height = 15;
        self.titleLab.nim_centerY = self.midDot.nim_centerY;
        self.titleLab.font = [UIFont systemFontOfSize:14];
        self.titleLab.textColor = HexColor(0x9C9DB3);
        
        self.timeLab.nim_height = 15;
        self.timeLab.nim_centerY = self.midDot.nim_centerY;
        self.timeLab.font = [UIFont systemFontOfSize:14];
        self.timeLab.textColor = HexColor(0x9C9DB3);
    }
}

@end
